/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.loading;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.chart.BarChart;
import twaver.network.CanvasPaintAdapter;
import twaver.network.TNetwork;
import demo.DemoPane;
import demo.chart.ChartPopupMenuGenerator;

public class LoadingDemo extends DemoPane implements ActionListener{

	private int stepCount = 5;
	private int unitNumber = 1000;
	
	private int currentStep = 0;
	private long startTime = -1;
	private Random random = new Random();
	private TDataBox box = new TDataBox();
	private Node boxLoad = new Node();
	private Node networkLoad = new Node();
	private TDataBox report = new TDataBox();
	private BarChart chart = new BarChart(report);
	private JTextField stepCountField = new JTextField(stepCount + "");
	private JTextField unitNumberField = new JTextField(unitNumber + "");
	private JButton button = new JButton("Press to Start Loading...");
	private TNetwork network = new TNetwork(box);
	
	private void afterPaintProcess(){
		if(startTime > 0){
			long time = System.currentTimeMillis() - startTime;
			networkLoad.addChartValue(time);
			
			currentStep--;
			startTime = -1;
			
			if(currentStep > 0){
				SwingUtilities.invokeLater(new Runnable(){
					public void run() {
						step();
					}
				});
			}else{
				button.setEnabled(true);
				stepCountField.setEnabled(true);
				unitNumberField.setEnabled(true);
			}
		}
	}
	
	public LoadingDemo(){
		network.addCanvasPaintListener(new CanvasPaintAdapter(){
			public void afterPaint(Graphics g) {
				afterPaintProcess();
			}
		});
		
		report.addElement(this.boxLoad);
		report.addElement(this.networkLoad);
		
		boxLoad.setName("DataBox Loading Time");
		networkLoad.setName("Network Loading Time");
		boxLoad.putChartColor(Color.BLUE.darker());
		networkLoad.putChartColor(Color.GREEN.darker());

		chart.setYAxisText("Time Consuming (ms)");
		chart.setTitle("TWaver Performamce Report");
		chart.setValueTextFont(TUIManager.getDefaultFont().deriveFont(9f));
		chart.setShadowOffset(0);
		chart.setSelectedOffset(0);
		chart.setBarType(TWaverConst.BAR_TYPE_GROUP);
		chart.setXScaleTextOrientation(TWaverConst.LABEL_ORIENTATION_RIGHT);
		chart.setXScaleTextColor(Color.RED);
		chart.setLazyPublishMode(true);
		
		chart.setPopupMenuGenerator(ChartPopupMenuGenerator.INSTANCE);
		
		button.addActionListener(this);
		button.setBackground(Color.ORANGE);
		
		stepCountField.setPreferredSize(new Dimension(80, 20));
		unitNumberField.setPreferredSize(new Dimension(80, 20));
		JPanel controlPane = new JPanel();
		controlPane.add(new JLabel("Step Count"));
		controlPane.add(stepCountField);
		controlPane.add(new JLabel("Unit Number"));
		controlPane.add(unitNumberField);
		controlPane.add(button);
		
    	JPanel pane = new JPanel(new GridLayout(2, 1));
    	pane.add(network);
    	pane.add(chart);
		this.add(pane, BorderLayout.CENTER);
		this.add(controlPane, BorderLayout.SOUTH);
	
	}

	public void actionPerformed(ActionEvent e) {
		button.setEnabled(false);
		stepCountField.setEnabled(false);
		unitNumberField.setEnabled(false);
		
		try{
			this.stepCount = Integer.parseInt(this.stepCountField.getText());
		}catch(Exception ex){
			this.stepCountField.setText(this.stepCount + "");
		}
		try{
			this.unitNumber = Integer.parseInt(this.unitNumberField.getText());
		}catch(Exception ex){
			this.unitNumberField.setText(this.unitNumber + "");
		}
		
		this.currentStep = stepCount;
		this.chart.setXScaleTextList(null);
		this.boxLoad.setChartValues(null);
		this.networkLoad.setChartValues(null);

		this.step();
	}
	
	private void step(){
		this.box.clear();
		
		this.startTime = System.currentTimeMillis();
		int sum = this.currentStep * this.unitNumber;
		for(int i=0; i<sum; i++){
			Node node = new Node();
			int n = sum > 800 ? sum : 800;
			node.setLocation(random.nextInt(n), random.nextInt(n));
			box.addElement(node);
		}
		long time = System.currentTimeMillis() - startTime;
		this.chart.addXScaleText(sum + "");
		this.boxLoad.addChartValue(time);
	}
	


}